package com.shangcheng.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.PmsProductCategoryApi;
import com.shangcheng.model.domain.PmsProductCategory;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.PmsProductCategoryVo;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PmsProductCategoryService {
    @DubboReference
    private PmsProductCategoryApi productCategoryApi;

    public PageResult list(Integer pageNum, Integer pageSize, Integer parentId) {
        PageResult result = new PageResult();
        IPage<PmsProductCategory> pg = productCategoryApi.pageList(pageNum, pageSize, parentId);

        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(pg.getRecords());
        result.setTotal(Convert.toInt(pg.getTotal()));
        result.setTotalPage(Convert.toInt(pg.getPages()));
        return result;
    }

    public List<PmsProductCategoryVo> withChildren() {
        List<PmsProductCategory> categoryList = productCategoryApi.withChildren(0L);
        List<PmsProductCategoryVo> list = new ArrayList<>();
        for (PmsProductCategory productCategory : categoryList) {
            List<PmsProductCategory> productCategoryList = productCategoryApi.withChildren(productCategory.getId());
            PmsProductCategoryVo vo = new PmsProductCategoryVo();
            BeanUtil.copyProperties(productCategory, vo);
            vo.setChildren(productCategoryList);
            list.add(vo);
        }
        return list;
    }

    public void create(PmsProductCategoryVo productCategoryVo) {
        PmsProductCategory productCategory = new PmsProductCategory();
        BeanUtil.copyProperties(productCategoryVo, productCategory);
        String s = productCategoryApi.create(productCategory, productCategoryVo.getProductAttributeIdList());
        if (!StrUtil.equals("ok", s)) {
            throw new BusinessException(s);
        }
    }

    public void update(Integer id, PmsProductCategoryVo productCategoryVo) {
        PmsProductCategory productCategory = new PmsProductCategory();
        BeanUtil.copyProperties(productCategoryVo, productCategory);
        productCategory.setId(Convert.toLong(id));
        String update = productCategoryApi.update(productCategory, productCategoryVo.getProductAttributeIdList());
        if (!StrUtil.equals("ok", update)) {
            throw new BusinessException(update);
        }
    }

    public void showStatus(Map params) {
        String ids = (String) params.get("ids");
        String showStatus = (String) params.get("showStatus");
        productCategoryApi.showStatus(Integer.parseInt(ids), Integer.parseInt(showStatus));
    }

    public void navStatus(Map params) {
        String ids = (String) params.get("ids");
        String navStatus = (String) params.get("navStatus");
        productCategoryApi.navStatus(Integer.parseInt(ids), Integer.parseInt(navStatus));
    }

    public PmsProductCategory findById(Integer id) {
        return productCategoryApi.findById(id);
    }

    public void deleteById(Integer id) {
        String delete = productCategoryApi.delete(id);
        if (!StrUtil.equals("ok", delete)) {
            throw new BusinessException(delete);
        }
    }
}
